<template>
  <div>

    <div class="mg-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" v-model="name"/>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="refresh">重置</el-button>
    </div>
    <div class="mg-10">
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd()">新增
      </el-button>
      <el-popconfirm
          style="margin: 0px 10px"
          confirm-button-text='确认'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="确定要批量删除吗？"
          @confirm="deleteBatch"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}"
              default-expand-all
              row-key="id">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="60px" v-if="false">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="200px">
      </el-table-column>
      <el-table-column prop="path" label="路径" width="100px">
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径" width="100px">
      </el-table-column>
      <el-table-column label="菜单图标" width="auto" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 26px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="菜单描述" width="auto">
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="350px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleAdd(scope.row.id)"
                     v-if="!scope.row.pid && !scope.row.path">新增子菜单
          </el-button>
          <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除吗？"
              @confirm="handleDelete(scope.row.id)"
          >
            <el-button
                size="mini"
                type="danger"
                slot="reference"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%"
               :before-close="dialogCancel">
      <el-form :model="menuForm" ref="menuForm" label-width="90px">
        <el-form-item label="菜单名称">
          <el-input v-model="menuForm.name"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="menuForm.path"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="menuForm.pagePath"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标">
            <el-select v-model="menuForm.icon" placeholder="请选择" style="width: 100%">
              <el-option
                  v-for="item in options"
                  :key="item.name"
                  :label="item.name"
                  :value="item.value">
                <i :class="item.value"/>{{item.name}}
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="菜单描述">
          <el-input v-model="menuForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCancel">取 消</el-button>
        <el-button type="primary" @click="saveMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Menu",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      menuForm: {},
      formLabelWidth: '120px',
      name: "",
      dialogFormVisible: false,
      options:[]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      axios.get("/menu", {
        params: {
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data
      })
    },
    handleAdd(pid) {
      this.dialogFormVisible = true
      this.menuForm = {}
      if (pid) {
        this.menuForm.pid = pid
      }
    },
    saveMenu() {
      axios.post("/menu/save", this.menuForm).then(res => {
        if (res) {
          this.$message.success("用户添加成功！")
        } else {
          this.$message.error("用户添加失败！")
        }
        this.dialogFormVisible = false
        this.menuForm = {}
        this.load()
      })
    },
    dialogCancel() {
      this.menuForm = {}
      this.dialogFormVisible = false
    },
    handleEdit(row) {
      //防止提前出现变化
      this.menuForm = {...row}
      this.dialogFormVisible = true
      axios.get("/menu/icons").then(res=>{
        console.log(res)
        this.options = res.data
      })
    },
    handleDelete(row) {
      axios.delete("/menu/del/" + row).then(res => {
        if (res) {
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      axios.post("/menu/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    refresh() {
      this.name = ""
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    addChildrenMenu(id) {

    }
  }
}
</script>

<style scoped>

</style>